package com.intexh.speedandroid.module.mine;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.intexh.speedandroid.R;
import com.intexh.speedandroid.base.BaseActivity;
import com.intexh.speedandroid.helper.ImageHelper;
import com.intexh.speedandroid.helper.UserHelper;
import com.intexh.speedandroid.net.Apis;
import com.intexh.speedandroid.net.NetworkManager;
import com.intexh.speedandroid.net.interfaces.OnRequestCallBack;
import com.intexh.speedandroid.utils.glide.GlideHelper;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EditUserInfoActivity extends BaseActivity {
    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.title_menu_tv)
    TextView titleSaveTv;
    @BindView(R.id.avatar_iv)
    ImageView avatarIv;
    @BindView(R.id.change_avatar_rlt)
    RelativeLayout changeAvatarRlt;
    @BindView(R.id.user_name_et)
    EditText userNameEt;
    private String compressPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_user_info);
        ButterKnife.bind(this);
        titleTv.setText("个人信息");
        userNameEt.setText(UserHelper.getUser().getUsername());
        GlideHelper.INSTANCE.loadCircleImage(avatarIv,UserHelper.getUser().getMember_avatar());
    }

    @OnClick({R.id.back_iv, R.id.title_menu_tv, R.id.change_avatar_rlt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.title_menu_tv:
                save();
                break;
            case R.id.change_avatar_rlt:
                ImageHelper.showAvatarChoose(this);
                break;
        }
    }

    private void save() {
        String s = userNameEt.getText().toString();
        if(TextUtils.isEmpty(s)){
            showToast("请输入昵称");
            return;
        }
        if(compressPath!=null){
            upLoadImage();
        }else{
            submit(UserHelper.getUser().getMember_avatar());
        }
    }

    private void upLoadImage() {
        showProgress();
        ImageHelper.upLoadImage(compressPath, new ImageHelper.OnUpLoadImageCallBack() {
            @Override
            public void onSuccess(String images) {
                hideProgress();
                submit(images);
            }

            @Override
            public void onFail(String error) {
                hideProgress();
                showToast(error);
            }
        });
    }

    private void submit(String member_avatar) {
        Map<String,String> params= new HashMap<>();
        params.put("member_avatar",member_avatar);
        params.put("member_name",userNameEt.getText().toString());
        showProgress();
        NetworkManager.INSTANCE.post(Apis.editMemberDataInfo, params, new OnRequestCallBack() {
            @Override
            public void onOk(String response) {
                hideProgress();
                showToast("修改成功");
                setResult(RESULT_OK);
                finish();
            }

            @Override
            public void onError(int code,String errorMessage) {
                hideProgress();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    List<LocalMedia> localMedias = PictureSelector.obtainMultipleResult(data);
                    compressPath = localMedias.get(0).getCompressPath();
                    GlideHelper.INSTANCE.loadCircleImage(avatarIv, compressPath);
                    break;
            }
        }
    }
}
